package com.winchain.chainbill.receivables.biz.impt.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.service.PubFileUploadService;
import com.shell.bee.file.vo.FileWapper;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.framework.utils.CommonFileUtil;
import com.winchain.chainbill.constant.ReceivablesConstants;
import com.winchain.chainbill.receivables.biz.impt.service.ReceivablesImptService;
import com.winchain.chainbill.receivables.biz.impt.vo.ReadEcho;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ReceivablesImptController extends BaseController {
	@Autowired
	private PubFileUploadService fileLocalUploadService;
	@Autowired
	private ReceivablesImptService receivablesImptService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "������������", methods = "����������������������")
	public ReadEcho read(String fileId) throws Exception {
		ReadEcho echo = this.receivablesImptService.read(this.fileLocalUploadService.getDownloadFile(fileId).getFile());
		if (StringUtil.notTrimEmpty(echo.getError())) {
			String error = CommonFileUtil.changMsg(echo.getError());
			echo.setError(error);
		}
		if (StringUtil.notTrimEmpty(echo.getWarning())) {
			String warning = CommonFileUtil.changMsg(echo.getWarning());
			echo.setWarning(warning);
		}
		return echo;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������������", methods = "��������������������")
	public RetMsg impt(String fileId) throws Exception {
		this.receivablesImptService.impt(this.fileLocalUploadService.getDownloadFile(fileId).getFile());
		return new RetMsg("success", false, "����������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public void downloadTemplate(String type, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String pathValue = "";
		String fileName = "";
		if ("FinRARecordImport".equals(type)) {
			pathValue = "/template/excel-import/FinRARecordImport.xlsx";
			fileName = "��������������������.xlsx";
		} else if ("FinRmAccessImport".equals(type)) {
			pathValue = "/template/excel-import/FinRmAccessImport.xlsx";
			fileName = "��������������������.xlsx";
		} else {
			return;
		}
		InputStream is = ReceivablesConstants.class.getResourceAsStream(pathValue);
		CommonFileUtil.writeFileToClient(fileName, is, response, request);
	}
}
